﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class AreaManagerController : BaseController<managerarea>
    {
        //
        // GET: /AreaManager/

        public override BLL.BaseBll<managerarea> bll
        {
            get
            {
                return new ManagerAreaBll();
            }

        }

        public class AreaviewModel
        {
            //   name  userName  userPass userPhone state areaName
              public int areaMaID { get; set; }
              public string  name{get;set;}
              public string  userName{get;set;}
              public string  userPass{get;set;}
              public string  userPhone{get;set;}
              public string  state{get;set;}
              public string  areaName{get;set;}
        }

        public class GetOneModel
        {
            //   name  userName  userPass userPhone state areaName
            public int areaMaID { get; set; }
            public string name { get; set; }
            public string userName { get; set; }
            public string userPass { get; set; }
            public string userPhone { get; set; }
            public string state { get; set; }
            public string areaName { get; set; }
            public string areaID { get; set; }
        }

        List<AreaviewModel> areaList = new List<AreaviewModel>();

        List<GetOneModel> oneList = new List<GetOneModel>();

        AreaBll areaBll = new AreaBll();

        public ActionResult Get2(int page, int rows, managerarea model)
        {
            int count;
            var list = bll.Search(model, x => true, true, x => x.areaMaID, rows, page, out count).ToList().Select(x => new
            {
                 x.areaMaID,
                x.name,
                x.userName,
                x.userPass,
                x.userPhone,
                x.areaCode,
                state = x.state == 1 ? "在职" : "离职",
            }).ToList();
            foreach (var item in list)
            {
                int code=Convert.ToInt32(item.areaCode);
                string areaName = areaBll.Search(x => x.areaCode == code).Select(x => new { x.areaName }).FirstOrDefault().areaName;
                AreaviewModel modelArea = new AreaviewModel();
                modelArea.areaMaID = item.areaMaID;
                modelArea.areaName = areaName;
                modelArea.name = item.name;
                modelArea.state = item.state;
                modelArea.userName = item.userName;
                modelArea.userPass = item.userPass;
                modelArea.userPhone = item.userPhone;
                areaList.Add(modelArea);
            }
            return Json(new { total = count, rows = areaList });
        }

        //获取选中的一条数据   
        public ActionResult GetOnlyOne(long id)
        {
           // return Json(bll.Search(x => x.areaMaID == id).ToList().Select(x => new { x.areaMaID,x.userPhone, x.name, x.userName, x.userPass, x.areaCode, }).ToList()[0], JsonRequestBehavior.AllowGet);
            var list = bll.Search(x => x.areaMaID == id).ToList().Select(x => new { x.areaMaID, x.userPhone, x.name, x.userName, x.userPass, x.areaCode, }).ToList();
            foreach (var item in list)
            {
                string areaName = areaBll.Search().Select(x => new { AreaCode = x.areaCode, AreaName = x.areaName }).ToList().FirstOrDefault().AreaName;
                GetOneModel model = new GetOneModel();
                model.areaMaID = list.FirstOrDefault().areaMaID;
                model.areaID = list.FirstOrDefault().areaCode;
                model.areaName = areaName;
                model.name = list.FirstOrDefault().name;
                model.userName = list.FirstOrDefault().userName;
                model.userPass=list.FirstOrDefault().userPass;
                model.userPhone = list.FirstOrDefault().userPhone;
                oneList.Add(model);

            }
            return Json(oneList.ToList()[0], JsonRequestBehavior.AllowGet);
        }


        public ActionResult GetArea()
        {
            return Json(areaBll.Search().Select(x => new { AreaCode = x.areaCode, AreaName = x.areaName }).ToList());

        }

        //新增用户
        [HttpPost]

        public ActionResult Save2(managerarea model)
        {
            string msg = "";
            int result = 0;
            int jieguo = 1;

            //模板设计器
            if (model.userName != "" && model.userName != "" && model.userPass != null && model.userPass != null)
            {
                var count = bll.Search(x => x.userName == model.userName).Count();
                //判断该用户是否存在  
                if (count == 0)
                {
                    model.state = 1;

                    if (bll.Add(model) > 0)
                    {
                        msg = "添加成功";
                        jieguo = 1;
                    }
                    else
                    {
                        jieguo = 1;
                        msg = "添加失败";

                    }
                }
                else
                {
                    jieguo = 0;
                    msg = "该账号已经存在，请重新填写";

                }
            }
            else
            {
                jieguo = 0;
                msg = "请填写完整";

            }
            return Json(new { jieguo = jieguo, state = result, msg = msg });
        }


        public ActionResult Modify2(managerarea model)
        {
            string msg = "修改失败";
            string state = "false";

            model.state = 1;

            if (model.userName != "" && model.userName !=null && model.userPass != "" && model.userPass != null)
            {
                if (bll.Modify(model) > 0)
                {
                    msg = "修改成功";
                    state = "true";
                }
            }
            else
            {
                msg = "请填写完整";
                state = "false";
            }

            return Json(new { state = state, msg = msg });
        }


        //禁用用户  修改状态为
        public ActionResult ModifyState(int id)
        {
            string msg = "禁用失败";
            string state = "false";

            var oneList = bll.Search(x => x.areaMaID == id).Select(x => new { x.areaMaID, x.areaCode, x.userPhone, x.name, x.state, x.userName,x.userPass,}).ToList();

            managerarea model = new managerarea();
            model.areaMaID = oneList.FirstOrDefault().areaMaID;
            model.userName = oneList.FirstOrDefault().userName;
            model.userPass = oneList.FirstOrDefault().userPass;
            model.userPhone = oneList.FirstOrDefault().userPhone;
            model.areaCode = oneList.FirstOrDefault().areaCode;
            model.name = oneList.FirstOrDefault().name;
            model.state = 0;

            if (bll.Modify(model) > 0)
            {
                msg = "禁用成功";
                state = "true";
            }
            return Json(new { state = state, msg = msg });
        }

    }
}
